// 转账功能
import { ref,reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { getPayInfo,sendYZM } from '@/utils/api'
import { type PayInfoInterface,type CardListInterface } from '@/utils/interface'

export default function(){
    const centerDialogVisible = ref(false)  // 确认信息弹窗
    const payBoxLoading       = ref(false) // 支付状态
    const codeCountDown       = ref(60)  // 验证码倒计时
    const formData = reactive<PayInfoInterface>({
        disabled        : false, // 是否禁用下拉框
        authcode        : '',    // 短信验证码
        txnamount       : '',    // 付款金额
        remark          : '',    // 付款说明
        submitLoading   : false, // 提交按钮loading状态
        selectVal       : null , // 当前选中的付款账号
        options         : []     // 账号列表
    });
    // 锁定下拉框
    let time :number; // 倒计时定时器
    // 账号查询
    function getPayInfoFunc(){
        payBoxLoading.value = true
        // 账号查询
        getPayInfo().then(res=>{
            console.log("账号查询:",res.data);
            
            if(res.data.code == 200){
                let arr :CardListInterface[] = []
                res.data.data.map((v:{name:string,code:string})=>{
                    arr.push({
                        label: v.name as string,
                        value: v.code as string
                    })
                })
                formData.options = arr
            }else{
                ElMessage({
                    message : res.data.msg,
                    type    : 'error',
                    duration: 1500
                })
            }
            payBoxLoading.value = false
        }).catch(err=>{
            ElMessage({
                message : err,
                type    : 'error',    
                duration: 1500        
            })
            payBoxLoading.value = false
        })
    }
    function changePayCard(){
        // 先判读是否选择了账号
        if(!formData.selectVal){
            ElMessage({
                message : '请选择提现账号',
                type    : 'error',
                duration: 2 * 1000
            })
            return false;
        }
        formData.disabled = true
        // console.log('提现账号',formData.selectVal);
        // 发送短信验证码
        sendYZM(formData.selectVal).then(res=>{
            // console.log(res);
            if(res.data.code == 200){
                ElMessage({
                    message : '短信验证码已发送，请注意查收',
                    type    : 'success',
                    duration: 2 * 1000
                })
                // 倒计时
                time = setInterval(()=>{
                    if(codeCountDown.value <= 0){
                        clearCountDown()
                    }else{
                        codeCountDown.value--
                    }
                },1000)
            }else{
                ElMessage({
                    message : res.data.msg,
                    type    : 'error',
                    duration: 1500
                })
                clearCountDown()
            }
        }).catch(err=>{
            ElMessage({
                message : err,
                type    : 'error',  
                duration: 15000          
            })
            clearCountDown()
        })
    }
    // 清除倒计时
    function clearCountDown(){
        clearInterval(time)
        formData.disabled   = false
        codeCountDown.value = 60
    }
    // 清空付款参数
    function clearPayInfo(){
        clearCountDown()
        formData.authcode   = ""
        formData.txnamount  = ""
        formData.remark     = ""
    }
    // 验证金额
    function validateMoney(){
        // 使用match方法提取所有匹配的数字
        let matches = formData.txnamount.match(/\d+(\.\d+)?/);
        console.log(matches);
        if(matches){
            // 判断金额是否大于500万
            formData.txnamount = Number(matches[0]) > 5000000 ? "5000000.00" : Number(matches[0]).toFixed(2)
        }else{
            formData.txnamount = ""
        }
    }
    // 验证信息
    function validateInfo():boolean{
        if (!formData.selectVal) {
            ElMessage({
                message : "请选择提现账号",
                type    : "error",
                duration: 2 * 1000
            });
            return false;
        }
        if (formData.authcode == "") {
            ElMessage({
                message : "请输入短信验证码",
                type    : "error",
                duration: 2 * 1000
            });
            return false;
        }
        if (formData.txnamount == "" || formData.txnamount == "0.00") {
            ElMessage({
                message : "请输入提现金额",
                type    : "error",
                duration: 2 * 1000
            });
            return false;
        }
        return true;
    }
    return {getPayInfoFunc,centerDialogVisible,formData,payBoxLoading,changePayCard,codeCountDown,clearPayInfo,validateMoney,validateInfo}
}